package com.wzh.live.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.wzh.live.conf.AliyunLiveConfig;
import com.wzh.live.entity.LiveHouseEntity;
import com.wzh.live.feign.HouseFeign;
import com.wzh.live.service.LiveHouseService;
import com.wzh.live.util.AliyunLiveUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wzh.live.entity.LiveStreamEntity;
import com.wzh.live.service.LiveStreamService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wzh
 * @email 1281204121@qq.com
 * @date 2022-03-28 17:09:10
 */
@Slf4j
@RestController
@RequestMapping("live/livestream")
public class LiveStreamController {
    @Autowired
    private LiveStreamService liveStreamService;

    @Autowired
    private LiveHouseService liveHouseService;

    @Resource
    private AliyunLiveConfig aliyunConfig;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = liveStreamService.queryPage(params);

        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{liveId}")
    public R info(@PathVariable("liveId") Integer liveId){
		LiveStreamEntity liveStream = liveStreamService.getById(liveId);
        liveStream.setNewHouseVos(liveHouseService.getsByLiveId(liveId));
        return R.ok().put("liveStream", liveStream);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody LiveStreamEntity liveStream){
        boolean save = liveStreamService.save(liveStream);
        if (save){
            //添加中间表
            List<LiveHouseEntity> collect = liveStream.getLiveHouses().stream().map(item -> {
                item.setLiveId(liveStream.getLiveId());
                return item;
            }).collect(Collectors.toList());
            liveHouseService.saveBatch(collect);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody LiveStreamEntity liveStream){
        boolean b = liveStreamService.updateById(liveStream);
        //删除中间表 根据LiveId
        if (b){
            liveHouseService.removeByLiveId(liveStream.getLiveId());

            //添加中间表
            List<LiveHouseEntity> collect = liveStream.getLiveHouses().stream().map(item -> {
                item.setLiveId(liveStream.getLiveId());
                return item;
            }).collect(Collectors.toList());
            liveHouseService.saveBatch(collect);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] liveIds){
        boolean b = liveStreamService.removeByIds(Arrays.asList(liveIds));
        //删除中间表 批量删除
        if(b){
            liveHouseService.removeByLiveIds(liveIds);
        }

        return R.ok();
    }

    /**
     * 生成推流播流地址
     * sourceId  在这里我将它设置为房间号
     */
    @GetMapping("/saveLive")
    public R save_Live(@RequestParam("sourceId") String sourceId)throws Exception{
        //生成推流地址
        String pushUrl = AliyunLiveUtil.createPushUrl(sourceId, aliyunConfig);
        //生成播流地址
        String pullUrl = AliyunLiveUtil.createPullUrl(sourceId, aliyunConfig);
        return R.ok().put("pushUrl",pushUrl).put("pullUrl",pullUrl);
    }

}
